async function PrintText ({ text = '', backspace = true, keepTime = 1000, intervel = 150, print = () => { } }) {
  return new Promise((resolve, reject) => {
    if (!(typeof (text) === 'string')) {
      reject(new Error('text 必须是字符串'))
    }
    var printText = ''
    var time = 0
    for (const char of text) {
      const value = printText + char
      const complate = value === text && !backspace
      printText = value
      setTimeout(() => {
        print(value, complate)
        if (complate) {
          resolve(complate)
        }
      }, time)
      time += intervel
    }
    if (backspace) {
      setTimeout(() => {
        while (printText !== '') {
          const value = printText.substring(0, printText.length - 1)
          const complate = value === ''
          printText = value
          setTimeout(() => {
            print(value, complate)
            if (complate) {
              resolve(complate)
            }
          }, time)
          time += intervel
        }
      }, keepTime)
    }
  })
}

async function Sleep (intervel) {
  return new Promise((resolve, reject) => {
    setTimeout(() => { resolve() }, intervel)
  })
}

export default PrintText
export {
  Sleep,
  PrintText
}
